package edu.gatech.cs2340.sharks.fms;

/**
 * ViewItemActivity.java
 * @author Miri Baker
 *
 * Shows the details of an item if a valid ID is returned by a call
 * to the database.
 * Otherwise, shows an error dialog and finishes the activity.
 */

import java.util.List;
import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class ViewItemActivity extends Activity
{
	/**
	 * the database ID of the item with which the activity was called.
	 */
	private int itemID;
	
	/**
	 * The actual item for the activity
	 */
	private Item item;
	
	/**
	 * The current instance of the activity, used to make sure there is only one instance of this activity running at a time.
	 */
	private static Activity instance = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		if (instance != null) instance.finish();
		instance = this;
		// Creates instance and sets layout, omitting title bar.
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_view_item);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras(); 
		itemID = extras.getInt("ItemID");
		
		// Gets the current ID'd item from the database.
		// Throws error dialog if id status code is not OK.
		Packet<Item> pi = DBMgr.getItem(itemID);
		if (pi.status.code() != Status.Code.OK) Info.alert(this, pi.status.toString());
		item = pi.value;
		
		// Gets all item TextViews to be set.
		TextView nameView = (TextView) findViewById(R.id.textViewName);
		TextView locView = (TextView) findViewById(R.id.textViewLoc);
		TextView dateView = (TextView) findViewById(R.id.textViewDate);
		TextView descView = (TextView) findViewById(R.id.textViewDesc);
		TextView rewardView = (TextView) findViewById(R.id.textViewReward);
		TextView statusView = (TextView) findViewById(R.id.textViewStatus);
		TextView tagView = (TextView) findViewById(R.id.textViewTags);
		
		// Sets non-situational text fields.
		nameView.setText(item.name);
		locView.setText(item.location);
		dateView.setText(item.date);
		descView.setText(item.description);
		
		// Builds a string for display of the item's tags.
		// Separated by commas for ease of reading.
		List<Tag> tags = item.tags;
		if (tags.size() > 0)
		{
			String tagsForView = "";
			for (int i = 0; i < tags.size() - 1; i++) tagsForView += tags.get(i).name + ", ";
			tagsForView += tags.get(tags.size() - 1).name;
			tagView.setText(tagsForView);
		}
		else tagView.setText(R.string.noTags);
		
		// Sets correct status text.
		if(item.status.equals(Item.Status.FOUND)) statusView.setText(R.string.item_status_found);
		else if(item.status.equals(Item.Status.LOST)) statusView.setText(R.string.item_status_lost);
		else if(item.status.equals(Item.Status.RESOLVED)) statusView.setText(R.string.item_status_resolved);
		
		// Sets correct reward amount text.
		String noreward = "No reward";
		String reward = "Reward: $";
		if(item.reward==0) rewardView.setText(noreward);
		else
		{
			reward+=String.valueOf(item.reward);
			rewardView.setText(reward);
		}
		
		// Obtains the user's items and compares it to the item being viewed.
		// If the user does not own the item being viewed, hide the edit item button
		Packet<List<Item>> userItems = DBMgr.userItems(DBMgr.user);
		List<Item> items = userItems.value;
		Button viewItem = (Button) findViewById(R.id.toEditItem);
		boolean own = false;
		for (Item i: items) if (i.id() == itemID) own = true;
		if (!own) viewItem.setVisibility(View.GONE);
	}

	/**
	 * Launches the EditItemActivity on button press.
	 * Adds the selected itemID to pass along.
	 * 
	 * @param view
	 */
	public void edit(View view)
	{
		Intent toEdit = new Intent(this, EditItemActivity.class);
		toEdit.putExtra("ItemID", itemID);
		startActivity(toEdit);
	}
	
	/**
	 * Allows the user to go back on button press.
	 * 
	 * @param view
	 */
	public void back(View view)
	{
		finish();
	}
}
